﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace RsaKeyCreater
{
    class Program
    {
        static void Main(string[] args)
        {
            //var rsa = new RSACryptoServiceProvider();
            //var privateParameters = rsa.ExportParameters(true);
            //var publicParameters = rsa.ExportParameters(false);

            ////Export private parameter XML representation of privateParameters
            ////object created above
            //File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "privatekey.xml", rsa.ToXmlString(true), Encoding.UTF8);
            var privateKey = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "privatekey.xml", Encoding.UTF8);
            //Console.WriteLine(privateKey);

            //Console.WriteLine("-------------------------------------");
            ////Export private parameter XML representation of publicParameters
            ////object created above
            //File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "publickey.xml", rsa.ToXmlString(false), Encoding.UTF8);
            var publicKey = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "publickey.xml", Encoding.UTF8);
            //Console.WriteLine(publicKey);

            //Console.WriteLine("-------------------------------------");

            var token = "jac:jac";

            var rsa1 = new RSACryptoServiceProvider();
            rsa1.FromXmlString(publicKey);
            var encryptedByteArray = rsa1.Encrypt(Encoding.UTF8.GetBytes(token), false);
            var enstring = Convert.ToBase64String(encryptedByteArray);
            Console.WriteLine(enstring);

            var rsa2 = new RSACryptoServiceProvider();
            rsa2.FromXmlString(privateKey);
            var decryptedByte = rsa2.Decrypt(Convert.FromBase64String(enstring), false);
            Console.WriteLine(Encoding.UTF8.GetString(decryptedByte));

            Console.Read();
        }
    }

}
